package com.houtai.controller;


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 分屏管理
 */
@RestController
@RequestMapping("sys/viewboxs")
public class ViewBoxs {

    @Autowired
    private JdbcTemplate jdbc;

    @RequestMapping(value = "/getall")
    public Object getall(){
        String  sql = "select * from myboxs order by id asc";
        return jdbc.queryForList(sql);
    }

    @RequestMapping(value = "/query/{id}",method = RequestMethod.GET)
    public Object querybyid(@PathVariable("id") Integer id){
        String  sql = "select * from myboxs where id = ? ";
        List<Map<String, Object>> maps = jdbc.queryForList(sql, id);
        return  maps;
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public Integer delete(@RequestBody Object obj){
        if(obj==null){
            System.out.println("obj为空");
            return -1;
        }
        Map mobj = (Map) obj;
        Integer id = Integer.parseInt(mobj.get("id").toString());
        String  sql = "delete from myboxs  where id = ?";
        return jdbc.update(sql,id);
    }

    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    public Integer insert(@RequestBody Object obj){
        if(obj==null){
            System.out.println("obj为空");
            return -1;
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmm");
        Date today = new Date();
        String todaystr = format.format(today);

        Map mobj = (Map)obj;
        String left = mobj.get("left").toString();
        String right = mobj.get("right").toString();
        String x = mobj.get("x").toString();
        String y = mobj.get("y").toString();
        String z = mobj.get("z").toString();
        String  sql = "insert into myboxs(leftchecked,rightchecked,datetime,x,y,z) values(?,?,?,?,?,?)";
        int insert = jdbc.update(sql, left, right,todaystr,x,y,z);
        return insert;
    }

}
